﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XAPI.Domain;
using XAPI.Data.Configuration;

namespace XAPI.Data
{
    public class VerbDataLoader : IDataLoader<Verb>
    {
        IRepository _repository;

        #region Constructors
        public VerbDataLoader(IRepository repository)
        {
            _repository = repository;
        }
        #endregion

        #region IDataLoader
        public void LoadData()
        {
            foreach (var item in GetVerbs())
            {
                _repository.AddOrModify<Verb>(item, item.Key);
            }
        }
        #endregion

        private IEnumerable<Verb> GetVerbs()
        {
            VerbConfigurationSection config =
        (VerbConfigurationSection)System.Configuration.ConfigurationManager.GetSection(
        "verbConfig");
            List<Verb> verbs = new List<Verb>();

            foreach (VerbElement item in config.Verbs)
            {
                var verb = new Verb();
                verb.Id = item.Id;

                LanguageMap map = new LanguageMap();

                foreach (var mapItem in item.Maps)
                {
                    map.Add(mapItem.Language, mapItem.Value);
                }

                verb.Display = map;
                verbs.Add(verb);
            }

            return verbs;
        }
    }
}
